package ift604.projet.feeling;

import ift604.projet.R;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FeelingSelectionActivity extends Activity {
    private feelingAdapter gridOfFeelingsAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.feeling_selection_panel);

        final GridView feelingsGrid = (GridView) findViewById(R.id.gridview);
        
        gridOfFeelingsAdapter = new feelingAdapter(Feeling.feelings, this);
        feelingsGrid.setAdapter(gridOfFeelingsAdapter);
        
        feelingsGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            	Feeling selected = (Feeling) (feelingsGrid.getItemAtPosition(pos));
            	
            	Intent data = new Intent();
            	data.setData(Uri.parse(selected.getName()));
            	
                setResult(RESULT_OK, data);
                FeelingSelectionActivity.this.finish();
            }
        });

    }
    
    class feelingAdapter extends BaseAdapter{
    	
    	private List<Feeling> feelingList;
    	private Context context;
    	
    	public feelingAdapter(List<Feeling> checkInList, Context context) {
        	
            this.feelingList = checkInList;
            this.context = context;
        }
    	
    	
		@Override
		public int getCount() {
			return feelingList.size();
		}

		@Override
		public Object getItem(int pos) {
			return feelingList.get(pos);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			FeelingView fv;
    		Feeling feeling = feelingList.get(position);
    		
    		fv = new FeelingView(context, feeling.getName(), feeling.getColor());
    		
    		return fv;
		}
    	
    }
    
    class FeelingView extends LinearLayout {
        public FeelingView(Context context, String title, int color) {
            super(context);
            this.setOrientation(VERTICAL);

            // Here we build the child views in code. They could also have
            // been specified in an XML file.
            String upperString = title.substring(0,1).toUpperCase() + title.substring(1).toLowerCase();
            mTitle = new TextView(context);
            mTitle.setPadding(20, 15, 20, 15);
            mTitle.setText(upperString);
            mTitle.setTextColor(Color.rgb(255, 255, 255));
            mTitle.setTextSize(21);
            
            //mTitle.setTextColor(color);
            mTitle.setBackgroundColor(color);
            
            LayoutParams ll = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            
            addView(mTitle, ll);
        }

        /**
         * Convenience method to set the title of a SpeechView
         */
        public void setTitle(String title) {
            mTitle.setText(title);
        }

        private TextView mTitle;
    }
}
